package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.query.StrategyQuery;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("strategy")
public class StrategyController {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IRegionService regionService;

    @RequestMapping("list")
    public String list(@ModelAttribute("qo") StrategyQuery qo, Model model) {
        model.addAttribute("pageInfo", strategyService.query(qo));
        model.addAttribute("regions", regionService.listAll(null));
        return "strategy/list";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(MultipartFile file, Strategy entity) {
        //如果有文件,就上传
        if (file != null && file.getSize() > 0) {
            String upload = UploadUtil.uploadQiniu(file);
            entity.setCoverUrl(UploadUtil.QINIU_PATH + "/" + upload);
        }
        strategyService.saveOrUpdate(entity);

        return new JSONResult();
    }
}
